var Dires_Utils = {};

Dires_Utils.init = function() {
};

Dires_Utils.scroll_and_focus = function(scroll_target, focus_target) {
	if (typeof scroll_target != 'undefined') {
		$.scrollTo($(scroll_target), 800);
	}
	if (typeof focus_target != 'undefined') {
		$(focus_target).focus();
	}
};


Dires_Utils.page_message = function(mess, mess_type) {
	var a = null;
	clearTimeout(a);
	var page_message = $('.page_message');
	page_message.html(mess);
	if (typeof mess_type == 'undefined' || mess_type == 1) {
		page_message.attr('class', 'page_message page_mess_error');
	} else if (mess_type == 2) {
		page_message.attr('class', 'page_message page_mess_ok');
	}
	page_message.slideDown(function() {
		a = setTimeout(function() { page_message.slideUp(); }, 4000);
	});
};


/**
 *	the confirmation popup: "are you sure blabla?" yes/no
 *	@param message the message to display
 *	@param function_yes the function to be called if the user clicks on the 'yes' button
 */
Dires_Utils.confirmation = function(message, function_yes, popup_element) {

	Dires_Utils.confirmation.yes_function = function_yes;
	if (popup_element === undefined) {
		popup_element = '#tailcast_confirmation';
	}
	var popup = $(popup_element);
	$('div h4', popup).html(message);
	popup.overlay({
		expose: {
			color:     '#555',
			loadSpeed: 100,
			opacity:   0.3
		},
		api: true,
		closeOnClick: false,
		closeOnEsc: false
	}).load();

	yes = function() {
		popup.overlay().close();
		Dires_Utils.confirmation.yes_function();
	}

	no  = function() {
		popup.overlay().close();
	}

	$('.yes', popup).live('click', function() {
		yes();
		return false;
	});
	$('.no',  popup).live('click', function() {
		no();
		return false;
	});
};


Dires_Utils.createCookie = function(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else {
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
};


Dires_Utils.readCookie = function(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') {
			c = c.substring(1, c.length);
		}
		if (c.indexOf(nameEQ) == 0) {
			return c.substring(nameEQ.length, c.length);
		}
	}
	return null;
};


/* live functions, they don't have to be inited on page ready */

// the overlay is applied live.
$('.overlay_trigger').live('click', function(e) {
	e.preventDefault();
	var pop = $(this).attr('rel');
	if (typeof pop != 'undefined' && pop != '' && $(pop).length > 0) {
		var closeOnClick = true;
		var closeOnEsc = true;
		var oneInstance = true;
		var mask = null;
		if ($(this).hasClass('modal')) {
			oneInstance = false;
			closeOnClick = false;
			closeOnEsc = false;
			mask = {
				color: '#ebecff',
				loadSpeed: 200,
				opacity: 0.7
			}
		}

		if ($(this).data('width') !== null) {
			$(pop).width($(this).data('width'));
		}

		var onBeforeLoad = null;
		var $me = $(this);

		if ($me.hasClass('ajax')) {
			onBeforeLoad = function() {
				var $inner = this.getOverlay().find(".inner");
				$inner.html('<img src="../images/loadingAnimation.gif">').load($me.attr('href'));
			}
		}

		$(pop).data('overlay', null);

		$(pop).overlay({
			api: true,
			fixed: false,
			oneInstance: oneInstance,
			closeOnClick: closeOnClick,
			closeOnEsc: closeOnEsc,
			mask: mask,
			onBeforeLoad: onBeforeLoad,
			onLoad: function() {
				if (typeof Dires_Utils.overlay_load == 'function') {
					Dires_Utils.overlay_load(this);
				}
			},
			onBeforeClose: function() {
				$('div.field_jserror').hide();
			}
		}).load();

		// hack to make overlay.getTrigger() return the correct trigger
		$(pop).data('overlay').getTrigger = function() {
			return $me;
		}
	}
});


$('a.jax').live('click', function(e) {
	e.preventDefault();

	var me = $(this);
	var href = me.attr('href');
	var $page = $('#ajax_page', me.attr('rel'));
	if ($page.length > 0) {
		$page.append('<img class="loading" src="../images/loadingAnimation.gif">');
	} else {
		$(me.attr('rel')).append('<img class="loading" src="../images/loadingAnimation.gif">');
	}
	$(me.attr('rel')).load(href).show();
	if (me.hasClass('add_title')) {
		$(document).attr('title', me[0].text);
	}

	if ($(this).hasClass('doscroll')) {
		Dires_Utils.scroll_and_focus($(this).attr('rel'));
	}
});


$('.toggler').live('click', function(e) {
	e.preventDefault();
	$(this).toggleClass('active');
	$($(this).attr('rel')).slideToggle(500);
});


$('#pattern_list a.pattern').live('click', function(e) {
	e.preventDefault();
	var $self = $(this);
	if ($self.hasClass('expanded')) {
		$self.toggleClass('expanded').next('div').hide();
	} else {
		var $nextdiv = $self.toggleClass('expanded').next('div');
		$nextdiv.html('<img class="loading" src="'+Dires_Utils.assets_url+'/images/ajax-loader.gif">').show();
		$.getJSON($self.attr('href')).success(function(data) {
			$nextdiv.html('');
			$('<table></table>').append($("#pattern_keys").tmpl( data )).appendTo($nextdiv);
		}).fail(function(data) {
			var message = 'Ummm...something bad happened. Unable to expand the pattern';
			if (data.status == 504) {
				message = 'Ughh, time out from server. Probably the pattern to expand is too big';
			}
			Dires_Utils.page_message(message, 1);
			$self.removeClass('expanded');
			$nextdiv.hide();
		});
	}
});

$('#db_change_form #db').live('change', function(e) {
	$('#db_change_form').submit();
});

$('.pattern_edit').live('click', function(e) {
	e.preventDefault();
	$('#overlay_editpattern input[name=old_key], #overlay_editpattern input[name=key]').val($(this).data('pattern'));
	$('#overlay_editpattern').overlay({api:true}).load();
});


/**
 *	on document ready. Keep as the last code in this file.
 */
$(function() {
	Dires_Utils.init();
});
